import Vue from "vue";
import store from "./../store";
import common from "../utils/common";
import Router from "vue-router";
import { Toast } from "vant";
import { bus } from "../utils/http";
import VueI18n from "vue-i18n";
import URL from "@/utils/url";
import { post } from "@/utils/http";

Vue.use(Router);
const originalPush = Router.prototype.push;
const originalReplace = Router.prototype.replace;
// push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch((err) => err);
};
// replace
Router.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalReplace.call(this, location, onResolve, onReject);
  return originalReplace.call(this, location).catch((err) => err);
};

const router = new Router({
  mode: "history",
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return {
        x: 0,
        y: 0,
      };
    }
  },
  routes: [
    {
      path: "/home",
      component: () => import("../views/home/index/index.vue"),
      meta: {
        title: "title.doc1",
        auth: true,
      },
    },
    {
      path: "/",
      component: () => import("../views/home/index/index.vue"),
      meta: {
        title: "title.doc1",
        auth: true,
      },
    },

    {
      path: "/user/login",
      component: () => import("../views/user/login"),
      meta: {
        title: "title.doc25",
      },
    },
    {
      path: "/user/reg",
      component: () => import("../views/user/reg"),
      meta: {
        title: "title.doc26",
      },
    },

    {
      path: "/user/forget_password",
      component: () => import("../views/user/forget_password"),
      meta: {
        title: "title.doc20",
      },
    },

    {
      path: "/my/index",
      component: () => import("../views/my/index"),
      meta: {
        title: "title.doc12",
      },
    },

    {
      path: "/wallet/index",
      component: () => import("../views/wallet/index"),
      meta: {
        title: "page4.doc31",
      },
    },
    {
      path: "/wallet/index1", // 电子钱包
      component: () => import("../views/wallet/index1"),
      meta: {
        title: "page4.doc30",
      },
    },
    {
      path: "/wallet/index2",
      component: () => import("../views/wallet/index2"),
      meta: {
        title: "title.doc27",
      },
    },
    {
      path: "/wallet/index3",
      component: () => import("../views/wallet/index3"),
      meta: {
        title: "page4.doc8",
      },
    },
    {
      path: "/wallet/coins",
      component: () => import("../views/wallet/coins"),
      meta: {
        title: "page4.doc1",
      },
    },
    {
      path: "/wallet/gold_description",
      component: () => import("../views/wallet/gold_description"),
      meta: {
        title: "page4.doc4",
      },
    },
    {
      path: "/wallet/tnsfer", // 划转
      component: () => import("../views/wallet/tnsfer"),
      meta: {
        title: "page3.doc75",
      },
    },

    {
      path: "/wallet/recharge",
      component: () => import("../views/wallet/recharge"),
      meta: {
        title: "title.doc28",
      },
    },
    {
      path: "/wallet/withdraw",
      component: () => import("../views/wallet/withdraw"),
      meta: {
        title: "title.doc29",
      },
    },
    {
      path: "/wallet/addressList",
      component: () => import("../views/wallet/addressList"),
      meta: {
        title: "page4.doc54",
      },
    },
    {
      path: "/wallet/bindAddress",
      component: () => import("../views/wallet/bindAddress"),
      meta: {
        title: "page4.doc58",
      },
    },

    {
      path: "/lucky",
      component: () => import("../views/lucky/index.vue"),
      meta: {
        title: "lucky.doc1",
      },
    },
    {
      path: "/lucky/go",
      component: () => import("../views/lucky/go.vue"),
      meta: {
        title: "lucky.doc10",
      },
    },
    {
      path: "/lucky/prize",
      component: () => import("../views/lucky/prize.vue"),
      meta: {
        title: "lucky.doc10",
      },
    },
    {
      path: "/lucky/record",
      component: () => import("../views/lucky/record.vue"),
      meta: {
        title: "lucky.doc23",
      },
    },
    {
      path: "/lucky/frequency/record",
      component: () => import("../views/lucky/frequencyRecord.vue"),
      meta: {
        title: "lucky.doc22",
      },
    },

    {
      // 删除账号
      path: "/my/delete_account",
      component: () => import("../views/my/delete_account"),
      meta: {
        title: "title.doc50",
      },
    },
    {
      path: "/my/password_login",
      component: () => import("../views/my/password_login"),
      meta: {
        title: "title.doc30",
      },
    },
    {
      path: "/my/password_trade",
      component: () => import("../views/my/password_trade"),
      meta: {
        title: "title.doc31",
      },
    },
    {
      path: "/my/invite",
      component: () => import("../views/my/invite"),
      meta: {
        title: "title.doc32",
      },
    },
    {
      path: "/my/userinfo",
      component: () => import("../views/my/userinfo.vue"),
      meta: {
        title: "title.doc67",
      },
    },

    {
      path: "/my/lang",
      component: () => import("../views/my/lang"),
      meta: {
        title: "title.doc33",
      },
    },
    {
      path: "/my/about",
      component: () => import("../views/my/about"),
      meta: {
        title: "title.doc34",
      },
    },
    {
      path: "/my/introduction",
      component: () => import("../views/my/introduction"),
      meta: {
        title: "page4.doc5",
      },
    },
    {
      path: "/my/real_authentication",
      component: () => import("../views/my/real_authentication"),
      meta: {
        title: "title.doc51",
      },
    },
    {
      path: "/my/prompt_success",
      component: () => import("../views/my/prompt_success"),
      meta: {
        title: "",
      },
    },
    {
      path: "/information/index",
      component: () => import("../views/information/index"),
      meta: { title: "title.doc35", auth: true },
    },
    {
      path: "/information/list",
      component: () => import("../views/information/list"),
      meta: { title: "title.doc35" },
    },
    {
      path: "/information/view",
      component: () => import("../views/information/view"),
      meta: { title: "title.doc36" },
    },

    {
      path: "/home/list",
      component: () => import("../views/home/list"),
      meta: { title: "title.doc1" },
    },
    {
      path: "/home/view",
      component: () => import("../views/home/view"),
      meta: { title: "title.doc1" },
    },
    {
      path: "/home/vrdetails",
      component: () => import("../views/home/vrdetails"),
      meta: { title: "title.doc1" },
    },
    {
      path: "/home/record",
      component: () => import("../views/home/record"),
      meta: { title: "title.doc48" },
    },
    {
      path: "/home/historyRecord",
      component: () => import("../views/home/historyRecord"),
      meta: { title: "title.doc49" },
    },
    {
      path: "/promote/index",
      component: () => import("../views/promote/index"),
      meta: { title: "title.doc37" },
    },
    {
      path: "/promote/team",
      component: () => import("../views/promote/team"),
      meta: { title: "new.doc4" },
    },
    {
      path: "/promote/details",
      component: () => import("../views/promote/details"),
      meta: { title: "title.doc44" },
    },

    {
      path: "/income/index",
      component: () => import("../views/income/index"),
      meta: { title: "title.doc40" },
    },
    {
      path: "/income/scenter", // 会员中心
      component: () => import("../views/income/scenter"),
      meta: { title: "title.doc45" },
    },
    {
      path: "/home/search",
      component: () => import("../views/home/search"),
      meta: { title: "title.doc41" },
    },
    {
      path: "/article",
      component: () => import("../views/article"),
      meta: { title: "title.doc41" },
    },
    {
      path: "/my/help",
      component: () => import("../views/my/help"),
      meta: { title: "title.doc42", keepAlive: "ture" },
    },
    {
      path: "/my/help_view",
      component: () => import("../views/my/help_view"),
      meta: { title: "title.doc43" },
    },
    {
      path: "/invest/index",
      component: () => import("../views/invest/index.vue"),
      meta: { title: "new.doc39" },
    },
    {
      path: "/invest/record",
      component: () => import("../views/invest/record.vue"),
      meta: { title: "page3.doc9" },
    },
    {
      path: "/invest/detail",
      component: () => import("../views/invest/detail.vue"),
      meta: { title: "page3.doc9" },
    },
    {
      path: "/invest/view",
      component: () => import("../views/invest/view.vue"),
      meta: { title: "page3.doc10" },
    },
    {
      path: "/my/download",
      component: () => import("../views/my/download.vue"),
      meta: "",
    },

    {
      path: "/financial/index",
      component: () => import("../views/financial/index.vue"),
      meta: { title: "page4.doc67" },
    },
    {
      path: "/financial/list",
      component: () => import("../views/financial/list.vue"),
      meta: { title: "page4.doc68" },
    },

    {
      path: "/financial/buy",
      component: () => import("../views/financial/buy.vue"),
      meta: { title: "page4.doc69" },
    },

    {
      path: "/financial/record",
      component: () => import("../views/financial/record.vue"),
      meta: { title: "page4.doc70" },
    },
    {
      path: "/iframe",
      component: () => import("../views/iframe"),
      meta: {
        title: "title.doc1",
      },
    },
    {
      path: "/customer",
      component: () => import("../views/iframe/customer.vue"),
      meta: {
        title: "page.doc99",
      },
    },
    {
      //路由重定向
      path: "*",
      redirect: "/home",
    },
  ],
});

router.beforeEach((to, from, next) => {
  let isLogin = store.state.isLogin;
  if (isLogin) {
    store.dispatch("Get_userData");
  }
  if (to.meta.auth) {
    if (!isLogin) {
      next("/user/login?status=1");
      return;
    }
  }
  const csUrl = store.getters.csUrl;
  if (to.path === "/customer" && !csUrl) {
    return;
  }
  //如果是详细页过来的，表明当前页面不需要刷新获取新数据，直接用之前缓存的数据即可
  if (
    from.name == "shop_detail" ||
    from.name == "shop_home" ||
    from.name == "mearch_alliance_view"
  ) {
    to.meta.isBack = true;
  }
  // if(to.path == '/wallet/withdraw'){
  //   bus.$emit('showrules')
  //   return;
  // }
  //bus.$emit('showrules')
  if (isLogin) {
    post(URL.home.taxinfo, {}).then((res) => {
      if (res.code == 0) {
        if (!res.data.hasOpen) {
          bus.$emit("showpopup", 0);
        } else if (to.path != "/wallet/recharge") {
          bus.$emit("showpopup", res.data);
        } else {
          bus.$emit("showpopup", 0);
        }
        next();
      } else if (res.code == 401) {
        store.dispatch("resetVuex");
        next("/user/login?status=1");
        return;
      } else {
        next();
      }
    });
  } else {
    next();
  }
});

router.onError((error) => {
  const isChunkLoadFailed = error.name === "ChunkLoadError";

  if (isChunkLoadFailed) {
    // 用路由的replace方法，并没有相当于F5刷新页面，失败的js文件并没有从新请求，会导致一直尝试replace页面导致死循环，而用 location.reload 方法，相当于触发F5刷新页面，虽然用户体验上来说会有刷新加载察觉，但不会导致页面卡死及死循环，从而曲线救国解决该问题
    location.reload();
  }
});
export default router;
